package com.muyu.vehicle.service.impl;

import java.util.List;

import com.muyu.vehicle.config.Config;
import com.muyu.vehicle.process.SynchronizeFenceInfoThread;
import com.muyu.vehicle.process.ThreadPool;
import com.vehicle.common.core.utils.DateUtils;
import com.vehicle.common.core.utils.IdUtils;
import com.vehicle.common.core.web.domain.AjaxResult;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.muyu.vehicle.mapper.VehicleFenceMapper;
import com.muyu.vehicle.domain.VehicleFence;
import com.muyu.vehicle.service.IVehicleFenceService;

/**
 * 电子围栏Service业务层处理
 *
 * @author muyu
 * @date 2021-09-04
 */
@Service
public class VehicleFenceServiceImpl implements IVehicleFenceService
{
    @Autowired
    private VehicleFenceMapper vehicleFenceMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 查询电子围栏
     *
     * @param id 电子围栏主键
     * @return 电子围栏
     */
    @Override
    public VehicleFence selectVehicleFenceById(String id)
    {
        return vehicleFenceMapper.selectVehicleFenceById(id);
    }

    /**
     * 查询电子围栏列表
     *
     * @param vehicleFence 电子围栏
     * @return 电子围栏
     */
    @Override
    public List<VehicleFence> selectVehicleFenceList(VehicleFence vehicleFence)
    {
        return vehicleFenceMapper.selectVehicleFenceList(vehicleFence);
    }

    /**
     * 新增电子围栏
     *
     * @param vehicleFence 电子围栏
     * @return 结果
     */
    @Override
    public int insertVehicleFence(VehicleFence vehicleFence)
    {
        vehicleFence.setCreateTime(DateUtils.getNowDate());
        vehicleFence.setId(IdUtils.fastSimpleUUID());
        try {
            return vehicleFenceMapper.insertVehicleFence(vehicleFence);
        }finally {
            ThreadPool.exeThread(new SynchronizeFenceInfoThread(redisTemplate , this , vehicleFence.getId()));
        }

    }

    /**
     * 修改电子围栏
     *
     * @param vehicleFence 电子围栏
     * @return 结果
     */
    @Override
    public int updateVehicleFence(VehicleFence vehicleFence)
    {
        try {
            vehicleFence.setUpdateTime(DateUtils.getNowDate());
            return vehicleFenceMapper.updateVehicleFence(vehicleFence);
        }finally {
            ThreadPool.exeThread(new SynchronizeFenceInfoThread(redisTemplate , this , vehicleFence.getId()));
        }
    }

    /**
     * 批量删除电子围栏
     *
     * @param ids 需要删除的电子围栏主键
     * @return 结果
     */
    @Override
    public int deleteVehicleFenceByIds(String[] ids)
    {
        try {
            return vehicleFenceMapper.deleteVehicleFenceByIds(ids);
        }finally {
            HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();
            hashOperations.delete(Config.VEHICLE_FENCE_INFO , ids);
        }
    }

    /**
     * 删除电子围栏信息
     *
     * @param id 电子围栏主键
     * @return 结果
     */
    @Override
    public int deleteVehicleFenceById(String id)
    {
        return vehicleFenceMapper.deleteVehicleFenceById(id);
    }

    @Override
    public String selectVehicleFencePathById(String id) {
        return vehicleFenceMapper.selectVehicleFencePathById(id);
    }

    @Override
    public void saveFencePath(String id, String path) {
        vehicleFenceMapper.saveFencePath(id , path);
    }
}
